/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 UJF.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.mediator.alarm.internal;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import org.osami.gateway.identity.Identity;
import org.osami.gateway.identity.IdentityProvider;
import org.osami.gateway.watteco.interfaces.IWattecoSmartplug;
import org.osgi.framework.ServiceReference;
import org.osgi.service.device.Constants;
import org.osgi.service.log.LogService;

import fr.liglab.adele.cilia.CiliaException;
import fr.liglab.adele.cilia.Data;

public class ProcessorAlarmEffector {

	private final String idLumiBelow = "LUMINOSITY_BELOW_THRESHOLD";

	private final String idLumiAbove = "LUMINOSITY_ABOVE_THRESHOLD";

	/**
	 * Logger, injected by iPOJO.
	 */
	private LogService logger;

	private Hashtable<String, IWattecoSmartplug> devices = new Hashtable<String, IWattecoSmartplug>();

	private IdentityProvider m_identityProvider;

	/**
	 * Check the temperature.
	 * 
	 * @throws CiliaException
	 */
	public List process(List dataset) throws CiliaException {
		for (Object d : dataset) {
			Data data = (Data) d;
			String alarmType = (String) data.getProperty("type");
			String alarmSource = (String) data.getProperty("source");
			if (idLumiBelow.equals(alarmType)) {
				System.out.println("below " + alarmType);
				startDeviceInLocation(alarmSource);
			} else {
				if (idLumiAbove.equals(alarmType)) {
					System.out.println("above " + alarmType);
					shutDeviceInLocation(alarmSource);
				}
			}
		}
		return dataset;
	}

	public void bindDevice(IWattecoSmartplug device,
			ServiceReference reference) {
		String id = (String) reference.getProperty(Constants.DEVICE_SERIAL);
		System.out.println("PowerSenseProducer: bindDevice" + reference
				+ " for ID " + id);
		devices.put(id, device);
	}

	public void unbindDevice(ServiceReference reference) {
		String id = (String) reference.getProperty(Constants.DEVICE_SERIAL);
		System.out.println("PowerSenseProducer: unbindDevice" + reference
				+ " for ID " + id);
		devices.remove(id);
	}

	private boolean shutDeviceInLocation(String location) {
		boolean res = false;
		for (String id : devices.keySet()) {
			Identity identity = m_identityProvider.getBoundIdentity(id);
			if (identity != null) {
				String loc = (String) identity.getMergedProperties().get(
						"room.name");
				if (loc != null) {
					if (loc.equals(location)) {
						String name = (String) identity.getMergedProperties()
								.get("identity.friendly.name");
						if (name.contains("Light")) {
							try {
								logger.log(LogService.LOG_INFO, "Switching off WattecoSmartPlug " + id);
								devices.get(id).switchOff();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

							res = true;
						}
					}
				} else {
					logger.log(LogService.LOG_WARNING, "Cannot get location");
				}
			} else {
				logger.log(LogService.LOG_WARNING, "Cannot get Identity ");
			}
		}
		return res;
	}

	private boolean startDeviceInLocation(String location) {
		boolean res = false;
		for (String id : devices.keySet()) {
			Identity identity = m_identityProvider.getBoundIdentity(id);
			if (identity != null) {
				String loc = (String) identity.getMergedProperties().get(
						"room.name");
				if (loc != null) {
					if (loc.equals(location)) {
						String name = (String) identity.getMergedProperties()
								.get("identity.friendly.name");
						if (name.contains("Light")) {
							try {
								logger.log(LogService.LOG_INFO, "Switching on WattecoSmartPlug " + id);
								devices.get(id).switchOn();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							res = true;
						}
					}
				} else {
					logger.log(LogService.LOG_WARNING, "Cannot get location");
				}
			} else {
				logger.log(LogService.LOG_WARNING, "Cannot get Identity ");
			}
		}
		return res;
	}

}
